public class LeetCode557 {
    public String reverseWords(String s) {
        String[] arr = s.split(" ");
        String[] temp = new String[arr.length];

        StringBuffer stringBuffer = new StringBuffer();

        for (int i = 0; i < arr.length; i++){
            if (i == arr.length -1){
                stringBuffer.append(reverseString(arr[i]));
            }else {
                stringBuffer.append(reverseString(arr[i])).append(" ");
            }
        }

        return stringBuffer.toString();
    }

    private String reverseString(String text) {
        char[] s = text.toCharArray();
        int startIndex = 0;
        int endIndex = s.length - 1;
        while (startIndex < endIndex){
            char temp;

            temp = s[startIndex];
            s[startIndex] = s[endIndex];
            s[endIndex] = temp;

            startIndex++;
            endIndex--;
        }

        return new String(s);
    }
}
